package com.spark.lola.logistics.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisManage {

    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    public RedisManage(RedisTemplate<String, String> systemRedis){
        this.redisTemplate =systemRedis;
    }
    // 缓存
    public void cache(String key, String value){
        redisTemplate.opsForValue().set(key, value);
    }

    // 缓存
    public void cache(String key, String value, long expiryTime, TimeUnit timeUnit){
        redisTemplate.opsForValue().set(key, value);
        redisTemplate.expire(key, expiryTime, timeUnit);
    }

    // 验证缓存是否已存在
    public Boolean validate(String key){
        return redisTemplate.hasKey(key);
    }

    // 获取缓存
    public String getValue(String key){
        return redisTemplate.opsForValue().get(key);
    }

    // 刷新缓存时间
    public Boolean refreshExpiryTime(String key, long expiryTime, TimeUnit timeUnit){
        return redisTemplate.expire(key,expiryTime,timeUnit);
    }

    // 删除缓存token
    public Boolean delete(String key){
        return redisTemplate.delete(key);
    }
}
